<template>
  <div>
    <div class="info">
      <div class="title">提现金额</div>
      <div class="num">
        <el-input type="number" v-model="money" placeholder="请输入提现金额"></el-input>
      </div>
      <div class="bottom">
        <p>
          当前可用余额：{{ user.money }}元
          <a class="tx" @click="allTx">全部提现</a>
        </p>
        <div class="btn" @click="onSubmit">确定</div>
      </div>
    </div>
    <div class="list">
      <div class="title">提现记录</div>
      <div class="col" v-for="item in list" :key="item.id">
        <div class="time">{{ item.desc }}</div>
        <div class="num">-{{ item.amount }}</div>
      </div>
    </div>
    <pagination
      :total="total"
      :currentPage="page"
      :pageSize="rows"
      @handleCurrentChange="handleCurrentChange"
    />
  </div>
</template>

<script>
export default {
  name: "NavBar",
  data() {
    return {
      user: "",
      money: 100,
      page: 1,
      total: 0,
      list: [],
      rows: 10,
    };
  },
  components: {
    pagination: () => import("@/components/pagination.vue"),
  },
  mounted() {
    this.getUser();
    this.getList();
  },
  methods: {
    getUser() {
      this.$post("/user/userinfo").then((res) => {
        this.user = res.data.userinfo;
      });
    },
    allTx() {
      if (parseFloat(this.user.money) > 0) {
        this.money = parseFloat(this.user.money);
      } else {
        this.$message.error("可用余额不足");
      }
    },
    onSubmit() {
      this.$confirm("您确认现在提现？", "提示信息", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          if (this.user.money == 0) {
            this.$message.error("可用余额不足");
            return;
          }
          if (!this.money) {
            this.$message.error("请输入提现金额");
            return;
          }
          if (this.money < 10) {
            this.$message.error("最低提现10元");
            return;
          }

          this.$post("/withdraw/submit", { money: this.money }).then((res) => {
            if (res.code == 1) {
              this.$message.success("提交成功");
              setTimeout(() => {
                this.getList();
              }, 1500);
            }
          });
        })
        .catch(() => {
          // 用户点击取消按钮或关闭对话框时执行
        });
    },
    getList() {
      this.$post("/withdraw/get_list", {
        page: this.page,
        rows: this.rows,
      }).then((res) => {
        this.list = res.data.data;
        this.total = res.data.total;
      });
    },
    handleCurrentChange(e) {
      this.page = e;
      this.getList();
    },
  },
};
</script>

<style scoped lang="scss">
.info {
  padding: 25px;
  .num {
    font-size: 60px;
    font-weight: bold;
    border-bottom: 2px dotted #999;
  }
  .bottom {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-top: 20px;
    .btn {
      color: #fff;
      background: #917bbd;
      line-height: 54px;
      width: 150px;
      text-align: center;
      border-radius: 20px;
      cursor: pointer;
    }
    .tx {
      color: #917bbd;
      cursor: pointer;
    }
  }
}
.list {
  background: #f4f4f4;
  padding: 25px;
  .title {
    font-size: 24px;
  }
  .col {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 20px 0;
    border-bottom: 1px dotted #999;
  }
}
@media screen and (max-width: 768px) {
}
</style>
